package com.wxtf.platform.main.proprietaryInquires;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.AccessDataBaseUtil;

/**
 * <li>Title: KeySerObjQuery.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.proprietaryInquires</li>
 * <li>Description: 重点服务对象名单</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Sep 21, 2012 10:29:02 AM</li>
 * 
 * @author wangzhi
 * @version 1.0.0.0
 * 
 */
public class KeySerObjQuery extends LogicBase implements CommonsCallProvider{
	private static final Log log = LogFactory.getLog(KeySerObjQuery.class);

	public KeySerObjQuery(String jsonArray){

	}
	
	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {
				msg = this.select(this.getLogicData());
				return msg;
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}

			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")){
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	String[] fields = {
			"reserve1","tbe_id","org_id","tbe_name","tbe_idcard","tbe_sex","tbe_marital_status","tbe_birth_control_measures","tbe_es_time","tbe_es_reason","tbe_address"
	};
	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String type= jsonObject.getString("type");
		String sql=" from tb_employee t1 where t1.tbe_is_es='1' and  t1.tbe_employment_forms <> '31' and t1.tbe_status not in ('102-3','102-4','102-5') ";
		if("init".equals(type)){
			sql = sql + " and t1.org_id in (" + user.getCurSubOrgId() + ") ";
		} else if("tj_ser".equals(type)) {
			String ser_from= jsonObject.getString("ser_from");
			String ser_to = jsonObject.getString("ser_to");
			String orgIds = jsonObject.getString("ser_org");
			if(StringUtils.isBlank(orgIds)){
				sql=sql+" and t1.org_id in ("+user.getCurSubOrgId()+") ";
			} else {
				sql=sql+" and t1.org_id in ("+user.getCurSubOrgIds(orgIds)+") ";
			}
			if(!StringUtils.isBlank(ser_from)){
				sql=sql+" and t1.tbe_birthday >= '"+ser_from+"' ";
			}
			if(!StringUtils.isBlank(ser_to)){
				sql=sql+" and t1.tbe_birthday <= '"+ser_to+"' ";
			}
		}
		// 排序
		String sortname = this.getRequest().getParameter("sortname");
		String sortorder = this.getRequest().getParameter("sortorder");
		if(sortname!=null&&sortname!=null) {
			sql += " order by " + sortname + " " + sortorder;
		}
		String countSql = "select count(*) " + sql;
		String querySql = "select t1.reserve1,t1.tbe_id,t1.org_id,t1.tbe_name,t1.tbe_idcard,t1.tbe_sex,t1.tbe_marital_status,t1.tbe_birth_control_measures,t1.tbe_es_time,t1.tbe_es_reason,t1.tbe_address " + sql;
        return AccessDataBaseUtil.query4SQLPagging(fields, countSql, querySql, this.getPage(), this.getPagesize(), null);
	}
     
	

	@Override
	public Object insert(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object update(Object jsonArray) throws Exception {
		return null;
	}
	
	@Override
	public Object delete(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}
}


